using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;

public class Validator : Singleton<Validator>
{
    public override void Init()
    {
        base.Init();
    }

    // unity ui component
    public List<string> unity_component = new List<string>()
    {
        "GameObject",
        "Transform",
        "Button",
        "Image",
        "Text",
        "RawImage",
        "Toggle",
        "Slider",
        "Scrollbar",
        "Dropdown",
        "InputField",
        "ScrollRect",
        "Mask",
        "LayoutElement",
        "ContentSizeFitter",
        "RectMask2D",
        "Shadow",
        "Outline",
        "PositionAsUV1",
        "VerticalLayoutGroup",
        "HorizontalLayoutGroup",
        "GridLayoutGroup",
        "CanvasRenderer",
        "Canvas",
        "RectTransform",
        "GraphicRaycaster",
        "CanvasScaler",
        "AspectRatioFitter",
        "Graphic",
        "Selectable",
        "StandaloneInputModule",
        "BaseInputModule",
        "EventSystem",
        "EventTrigger",
        "BaseRaycaster",
        "Physics2DRaycaster",
        "PhysicsRaycaster",
        "MaskableGraphic",
        "TextMesh",
        "MeshRenderer",
        "MeshFilter",
        "Mesh",
        "BoxCollider",
        "SphereCollider",
        "CapsuleCollider",
        "MeshCollider",
        "WheelCollider",
        "Rigidbody",
        "Rigidbody2D",
        "Animator",
        "Animation",
        "AudioSource",
        "AudioListener",
        "Light",
        "Camera",
        "FlareLayer",
        "GUILayer",
        "Skybox",
        "Terrain",
        "TerrainCollider",
        "TerrainData",
        "Tree",
        "WindZone",
        "Halo",
        "HaloLayer",
        "Projector",
        "LineRenderer",
        "TrailRenderer",
        "ParticleRenderer",
        "ParticleAnimator",
        "ParticleEmitter",
        "ParticleSystem",
        "LODGroup",
        "SpriteRenderer",
        "SpriteMask",
        "WorldParticleCollider",
    };

    // public void WriteUnityType()
    // {
    //     var sb = new System.Text.StringBuilder();
    //     var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    //     foreach(var asm in assemblies)
    //     {
    //         var types = asm.GetTypes();
    //         foreach(var t in types)
    //         {
    //             if(t.Namespace != null)
    //             {
    //                 // unity_component.Add(t.Name);
    //                 // sb.AppendLine(t.Name);
    //                 if(t.Namespace.Contains("UnityEngine") || t.Namespace.Contains("UnityEngine.UI"))
    //                 {
    //                     sb.AppendLine(t.Name);
    //                 }
    //             }
    //         }
    //     }

    //     var types_str = sb.ToString();
    //     var sr = new System.IO.StringReader(types_str);
    //     var line = "";
    //     var tmp = "";
    //     while((line = sr.ReadLine()) != null)
    //     {
    //         // Debug.Log(line);
    //         if(line.Contains("<") || line.Contains(">") || line.Contains("`"))
    //         {
    //             continue;
    //         }
    //         tmp += line + "\n";
    //     }
    //     var filePath = "Assets/Scripts/Editor/Config/Test/Test.txt";
    //     if(!System.IO.File.Exists(filePath))
    //     {
    //         var f = System.IO.File.Create(filePath);
    //         f.Close();
    //     }

    //     var file = System.IO.File.Open(filePath, System.IO.FileMode.OpenOrCreate);
    //     file.SetLength(0);
    //     var bytes = System.Text.Encoding.UTF8.GetBytes(tmp);
    //     file.Write(bytes, 0, bytes.Length);
    //     file.Close();
    // }

    // validate the type
    public bool ValidateType(string type , string nameSpace = "")
    {

        // is unity component
        if(unity_component.Contains(type))
        {
            return true;
        }

        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        foreach(var asm in assemblies)
        {
            var types = asm.GetTypes();
            foreach(var t in types)
            {
                if(t.Name == type)
                {
                    if(nameSpace != "")
                    {
                        if(t.Namespace != nameSpace)
                            continue;
                    }
                    Debug.Log(asm.GetName() + " : " + t.Name + " : " + t.Namespace);
                    return true;
                }
            }
        }

        return false;

    }

    public Type GetValidateType(string type,string nameSpace = "")
    {
        // get all assembly
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        foreach(var assembly in assemblies)
        {
            var types = assembly.GetTypes();
            foreach(var t in types)
            {
                if(t.Name == type)
                {
                    if(nameSpace != "")
                    {
                        if(t.Namespace != nameSpace)
                            continue;
                    }
                    return t;
                }
            }
        }

        return null;
    }


}